package com.lovo.bbs.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.bbs.bo.ForumBo;
import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.TopicBo;
import com.lovo.bbs.bo.UserInfoBo;
import com.lovo.bbs.util.DateUtil;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.ForumVo;
import com.lovo.bbs.vo.TopicVo;

public class NewTopicServlet extends HttpServlet {
	private static final long serialVersionUID = -1290094753285241861L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@SuppressWarnings("unchecked")
	protected synchronized void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//取参
		String forumid = req.getParameter("forumid");
		String topicTitle = new String(req.getParameter("topictitle").getBytes("ISO-8859-1"),"utf-8");
		String topicAuthorName = new String(req.getParameter("topicauthorname").getBytes("ISO-8859-1"),"utf-8");
		String topicContent = new String(req.getParameter("topiccontent").getBytes("ISO-8859-1"),"utf-8");
		//封装主题VO
		TopicVo vo = new TopicVo();
		vo.setForumid(Integer.parseInt(forumid));
		vo.setTopicTitle(topicTitle);
		vo.setAuthorname(topicAuthorName);
		vo.setContent(topicContent);
		
		ServletContext context = req.getSession().getServletContext();
		
		//更新主题表
		int topicid = new TopicBo().addOneTopic(vo);
		
		//更新数据库论坛表当前论坛信息
		new ForumBo().addOneTopic(topicid,Integer.parseInt(forumid));
		//更新内存中当前论坛信息
		ArrayList<ForumVo> forumList = (ArrayList<ForumVo>) context
				.getAttribute("forumList");
		Iterator<ForumVo> it = forumList.iterator();
		ForumVo thisForum = null;
		while(it.hasNext()){
			thisForum = it.next();//取出该论坛
			if(thisForum.getForumID()==Integer.parseInt(forumid)){
				break;
			}
		}
		//午夜时今日主题数清零,可能导致误差,当有新主题发表时,误差会消除
		if("00:00:00".equals(DateUtil.getTime())){
			thisForum.setTodayTopicNum(0);
		}
		thisForum.setLastTopicDate(DateUtil.getDateTime());
		
		thisForum.setLastTopicID(topicid);
		thisForum.setLastTopicTitle(topicTitle);
		thisForum.setLastTopicAuthor(topicAuthorName);
		thisForum.setTodayTopicNum(thisForum.getTodayTopicNum()+1);
		thisForum.setTopicNum(thisForum.getTopicNum()+1);
		
		context.setAttribute("forumList", forumList);
		
		HashMap<Integer,ForumVo> forumMap = (HashMap<Integer,ForumVo>)context.getAttribute("forumMap");
		forumMap.put(thisForum.getForumID(), thisForum);
		context.setAttribute("forumMap", forumMap);
		
		//更新网站杂项信息表
		new ForumStatBo().addOneTopic();
		//更新内存中网站杂项信息
		ForumStatVo forumInfo = (ForumStatVo)context.getAttribute("forumInfo");
		//午夜时今日主题数清零,可能导致误差,当有新主题发表时,误差会消除
		if("00:00:00".equals(DateUtil.getTime())){
			forumInfo.setTodayTopicNum(0);
		}
		forumInfo.setTodayTopicNum(forumInfo.getTodayTopicNum()+1);
		forumInfo.setTopicNum(forumInfo.getTopicNum()+1);
		
		context.setAttribute("forumInfo", forumInfo);
		
		//更新用户表
		new UserInfoBo().addOneTopic(topicAuthorName);
		
		//转向至新主题页
        if(topicid!=0){
        	resp.sendRedirect("topic.jsp?tid="+topicid);
        }
	}

}
